<template>
  <!-- 对话框表单 -->
  <el-dialog class="ba-operate-dialog"
             :close-on-click-modal="false"
             :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
             @close="baTable.toggleForm"
             :destroy-on-close="true">
    <template #header>
      <div class="title"
           v-drag="['.ba-operate-dialog', '.el-dialog__header']"
           v-zoom="'.ba-operate-dialog'">
        {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
      </div>
    </template>
    <el-scrollbar v-loading="baTable.form.loading"
                  class="ba-table-form-scrollbar">
      <div class="ba-operate-form"
           :class="'ba-' + baTable.form.operate + '-form'"
           :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
        <el-form ref="formRef"
                 @keyup.enter="baTable.onSubmit(formRef)"
                 :model="baTable.form.items"
                 :label-position="config.layout.shrink ? 'top' : 'right'"
                 :label-width="baTable.form.labelWidth + 'px'"
                 v-if="!baTable.form.loading">
          <FormItem :label="t('order.orderShip.order_id')"
                    v-model="baTable.form.items!.order_id"
                    :input-attr="{
                            disabled: true,
                        }"
                    type="string"
                    prop="order_id"
                    :placeholder="t('Please input field', { field: t('order.orderShip.order_id') })" />
          <FormItem :label="t('order.orderShip.tracking_number')"
                    v-model="baTable.form.items!.tracking_number"
                    :input-attr="{
                            disabled: true,
                        }"
                    type="string"
                    prop="tracking_number"
                    :placeholder="t('Please input field', { field: t('order.orderShip.tracking_number') })" />

          <FormItem :label="t('order.orderShip.buyer_email')"
                    v-model="baTable.form.items!.buyer_email"
                    type="string"
                    prop="append_buyer_email"
                    :placeholder="t('Please input field', { field: t('order.orderShip.buyer_email') })" />
          <FormItem :label="t('order.orderShip.postal_code')"
                    v-model="baTable.form.items!.postal_code"
                    type="string"
                    prop="append_postal_code"
                    :placeholder="t('Please input field', { field: t('order.orderShip.postal_code') })" />
          <FormItem :label="t('order.orderShip.buyer_name')"
                    v-model="baTable.form.items!.name"
                    type="string"
                    prop="append_name"
                    :placeholder="t('Please input field', { field: t('order.orderShip.buyer_name') })" />
          <FormItem :label="t('order.orderShip.phone_number')"
                    v-model="baTable.form.items!.phone_number"
                    type="string"
                    prop="append_phone_number"
                    :placeholder="t('Please input field', { field: t('order.orderShip.phone_number') })" />
          <FormItem :label="t('order.orderShip.region_code')"
                    v-model="baTable.form.items!.region_code"
                    type="string"
                    prop="append_region_code"
                    :placeholder="t('Please input field', { field: t('order.orderShip.region_code') })" />
          <FormItem :label="t('order.orderShip.country')"
                    v-model="baTable.form.items!.country"
                    type="string"
                    prop="append_country"
                    :placeholder="t('Please input field', { field: t('order.orderShip.country') })" />

          <FormItem :label="t('order.orderShip.state')"
                    v-model="baTable.form.items!.state"
                    type="string"
                    prop="append_state"
                    :placeholder="t('Please input field', { field: t('order.orderShip.state') })" />
          <FormItem :label="t('order.orderShip.city')"
                    v-model="baTable.form.items!.city"
                    type="string"
                    prop="append_city"
                    :placeholder="t('Please input field', { field: t('order.orderShip.city') })" />
          <FormItem :label="t('order.orderShip.county')"
                    v-model="baTable.form.items!.county"
                    type="string"
                    prop="append_county"
                    :placeholder="t('Please input field', { field: t('order.orderShip.county') })" />
          <FormItem :label="t('order.orderShip.address_detail')"
                    v-model="baTable.form.items!.address_detail"
                    type="string"
                    prop="append_address_detail"
                    :placeholder="t('Please input field', { field: t('order.orderShip.address_detail') })" />
          <FormItem :label="t('order.orderShip.shipping_documents')"
                    v-model="baTable.form.items!.shipping_documents"
                    type="string"
                    prop="shipping_documents"
                    :placeholder="t('Please input field', { field: t('order.orderShip.shipping_documents') })" />
          <FormItem label="SKU(多个sku用英文逗号分隔)"
                    v-model="baTable.form.items!.skus_info"
                    type="textarea"
                    :input-attr="{
                        rows: 3,
                    }"
                    prop="skus_info"
                    :placeholder="t('Please input field', { field: 'sku' })" />
        </el-form>
      </div>
    </el-scrollbar>
    <template #footer>
      <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
        <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
        <el-button v-blur
                   :loading="baTable.form.submitLoading"
                   @click="baTable.onSubmit(formRef)"
                   type="primary">
          {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, inject, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import type { FormInstance, FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { useAdminInfo } from '/@/stores/adminInfo'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const adminInfo = useAdminInfo()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

// const rules: Partial<Record<string, FormItemRule[]>> = reactive({
//     name: [buildValidatorData({ name: 'required', title: t('shop.shop.name') })],
//     region: [buildValidatorData({ name: 'required', title: t('shop.shop.region') })],
//     seller_type: [buildValidatorData({ name: 'required', title: t('shop.shop.seller_type') })],
// })

watch(
    () => baTable.form.operate,
    (newVal) => {
        //rules.mobile![0].required = newVal == 'Add'
    }
)
</script>